C# winform中 窗体缩放自适应的方法(不同电脑/不同分辨率)
sslyc8991:
缩放:
[code=csharp]
public void Scale()
{
_ = Form ?? throw new ArgumentNullException(nameof(Form), "请先调用Init进行初始化");
var scaleX = (double)Form.Width / InitWidth;
var scaleY = (double)Form.Height / InitHeight;
void ControlsChangeRec(Control ctrl)
{
foreach (Control item in ctrl.Controls)//遍历控件
{
if (item.Name.Trim() != "")//如果控件名不是空,则执行
{
if ((item as UserControl) == null && item.Controls.Count > 0)//如果不是自定义控件
{
ControlsChangeRec(item);
}
var (left, top, width, height, fontSize) = ControlsInfo[item.Name];
double itemWidth = width * scaleX;
double itemHeight = height * scaleY;
item.Left = (int)(left * scaleX - itemWidth / 2); //距离左边距离
item.Top = (int)(top * scaleY - itemHeight / 2); //距离顶部距离
[/code]
|